package com.good.jzhouse.controller;

import com.good.jzhouse.bean.Users;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("upimg")
public class UpImgController {

    @RequestMapping("/upload")
    @ResponseBody
    public synchronized Map upload(@RequestParam MultipartFile file, HttpServletRequest request){
        HttpSession s = request.getSession();
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                //文件名
                String filename = file.getOriginalFilename();
                //上传路径
                String property = System.getProperty("user.dir");
                String filepath = property+"\\jzhouse\\src\\main\\resources\\static\\images\\houseimg\\"+((Users)s.getAttribute("user")).getUserId()+""+filename;
                File files=new File(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("src",filepath);
                String imgPaths = (String) s.getAttribute("imgPaths");
                String uid = ((Users) s.getAttribute("user")).getUserId();
                if(imgPaths==null){
                    imgPaths=uid+filename;
                }else{
                    imgPaths=imgPaths+","+uid+filename;
                }
                s.setAttribute("imgPaths",imgPaths);//保存文件名
                return map;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;
    }
    @RequestMapping("/upload2")
    @ResponseBody
    public synchronized Map upload2(@RequestParam MultipartFile file, HttpServletRequest request){
        HttpSession s = request.getSession();
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                //文件名
                String filename = file.getOriginalFilename();
                //上传路径
                String property = System.getProperty("user.dir");
                String filepath = property+"\\jzhouse\\src\\main\\resources\\static\\images\\userimg\\"+((Users)s.getAttribute("user")).getUserId()+""+filename;

                File files=new File(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("src",filepath);
                String imgPaths = (String) s.getAttribute("imgPaths");
                String uid = ((Users) s.getAttribute("user")).getUserId();
                s.setAttribute("userimgPaths",uid+filename);//保存文件名
                System.out.println(1111111);
                return map;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;
    }

}
